package com.ronglian.register_mini_program.service.impl;

import com.ronglian.register_mini_program.dao.RegisterUserMapper;
import com.ronglian.register_mini_program.dao.UserFormMapper;
import com.ronglian.register_mini_program.model.RegisterUser;
import com.ronglian.register_mini_program.model.RegisterUserExample;
import com.ronglian.register_mini_program.model.UserForm;
import com.ronglian.register_mini_program.model.UserFormExample;
import com.ronglian.register_mini_program.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    RegisterUserMapper userMapper;
    @Autowired
    UserFormMapper formMapper;


    @Override
    public RegisterUser findByOpenId(String openid) {
        RegisterUserExample registerUserExample = new RegisterUserExample();
        RegisterUserExample.Criteria criteria = registerUserExample.createCriteria();
        criteria.andOpen_idEqualTo(openid);
        List<RegisterUser> registerUserList = userMapper.selectByExample(registerUserExample);
        if (registerUserList != null && ((java.util.List) registerUserList).size() > 0) {
            return registerUserList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public int insert(RegisterUser user) {
        return userMapper.insert(user);
    }

    @Override
    public int update(RegisterUser user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public int addFormId(UserForm record) {
        return formMapper.insert(record);
    }

    @Override
    public List<UserForm> getFormIdListWithOpenId(String open_id) {

        UserFormExample userFormExample = new UserFormExample();
        UserFormExample.Criteria ufc = userFormExample.createCriteria();
        if (StringUtils.isNotBlank(open_id)) {
            ufc.andOpen_idEqualTo(open_id);
        }
        ufc.andIs_usedEqualTo(0);
        userFormExample.setOrderByClause("create_time asc");
        List<UserForm> forms = formMapper.selectByExample(userFormExample);
        if (forms!=null&&forms.size()>0){
            return forms;
        }

        return null;
    }

}
